//
//  PushDataManager.m
//  leo
//
//  Created by 常艳 on 15/11/11.
//  Copyright (c) 2015年 mdx. All rights reserved.
//

#import "PushDataManager.h"

@implementation PushDataManager
+(PushDataManager *)sharedSingleton
{
    static PushDataManager *pushDataManager;
    @synchronized(self)
    {
        if(pushDataManager==NULL)
            pushDataManager=[[PushDataManager alloc] init];
    }
    return pushDataManager;
}
-(id)init
{
    self=[super init];
    if(self.theHelper==NULL)
    {
        self.theHelper=[[DBHelper alloc] init];
    }   
    return  self;
}
-(void)dealloc
{
    self.theHelper=NULL;
}
-(BOOL) checkValid
{
    if(self.theHelper==NULL)
    {
        NSLog(@"%@,checkValid,theHelper is null",PushDataManager_Tag);
        return NO;
    }
    return YES;
}
-(BOOL)addBindRequest:(PMessage *)message
{
    NSLog(@"%@,addBindRequest entered.",PushDataManager_Tag);
    if([self checkValid]==NO)
        return NO;
    if(message==NULL)
    {
        NSLog(@"%@,addBindRequest,message is null.",PushDataManager_Tag);
        return NO;
    }
    NSString *nsql=[NSString stringWithFormat:@"select * from message where uid=%@ and deviceUid=%@ and value=0;",message.uid,message.deviceUid];
    NSArray *data=[self.theHelper searchData:[nsql UTF8String]];
    if(data==NULL||[data count]==0)
    {
        NSString *ts=[Tooler getTS];
        NSString *nsql1=[NSString stringWithFormat:@"insert into message(msg_type,random_id,deviceUid,uid,nick_name,alias,create_time,account_id,random_code) values (%d,%@,%@,%@,\"%@\",\"%@\",\"%@\",%@,\"%@\");",message.type,message.randomId,message.deviceUid,message.uid,message.nickName,message.alias,ts,message.accountId,message.randomCode];
        BOOL result1=[self.theHelper insertData:[nsql1 UTF8String] tableName:TABLE_MESSAGE];
        if(result1)
        {
            NSLog(@"%@,addBindRequest success.",PushDataManager_Tag);
        }
        else
        {
            NSLog(@"%@,addBindRequest fail.",PushDataManager_Tag);
        }
        return result1;
    }
    return YES;
}
-(BOOL)addFenceInOut:(PMessage *)message
{
    NSLog(@"%@,addFenceInOut entered.",PushDataManager_Tag);
    if([self checkValid]==NO)
        return NO;
    if(message==NULL)
    {
        NSLog(@"%@,addFenceInOut,message is null.",PushDataManager_Tag);
        return NO;
    }
    NSString *ts=[Tooler getTS];
    NSString *nsql=[NSString stringWithFormat:@"insert into message (msg_type,random_id,deviceUid,title,create_time,fence_id,loc_id,fen_type,account_id,update_time) values (%d,%@,%@,\"%@\",\"%@\",%@,%d,%d,%@,\"%@\");",message.type,message.randomId,message.deviceUid,message.title,ts,message.fid,message.locId,message.fenceType,message.accountId,message.updateTime];
    BOOL result=[self.theHelper insertData:[nsql UTF8String] tableName:TABLE_MESSAGE];
    if(result)
    {
        NSLog(@"%@,addFenceInOut success.",PushDataManager_Tag);
    }
    else
    {
        NSLog(@"%@,addFenceInOut fail.",PushDataManager_Tag);
    }
    return result;
}
-(BOOL)addSos:(PMessage *)message
{
    NSLog(@"%@,addSos entered.",PushDataManager_Tag);
    if([self checkValid]==NO)
        return NO;
    if(message==NULL)
    {
        NSLog(@"%@,addSos,message is null.",PushDataManager_Tag);
        return NO;
    }
    NSString *nsql=[NSString stringWithFormat:@"insert into message (deviceUid,loc_id,account_id,msg_type,create_time) values (%@,%d,%@,%d,\"%@\");",message.deviceUid,message.locId,message.accountId,message.type,[Tooler getTS]];
    BOOL result=[self.theHelper insertData:[nsql UTF8String] tableName:TABLE_MESSAGE];
    if(result)
    {
        NSLog(@"%@,addSos success.",PushDataManager_Tag);
    }
    else
    {
        NSLog(@"%@,addSos fail.",PushDataManager_Tag);
    }
    return result;
}
-(BOOL)addLowBattery:(PMessage *)message
{
    NSLog(@"%@,addLowBattery entered.",PushDataManager_Tag);
    if([self checkValid]==NO)
        return NO;
    if(message==NULL)
    {
        NSLog(@"%@,addLowBattery,message is null.",PushDataManager_Tag);
        return NO;
    }
    NSString *nsql=[NSString stringWithFormat:@"insert into message(deviceUid,loc_id,account_id,battery,msg_type,create_time) values (%@,%d,%@,%d,%d,\"%@\");",message.deviceUid,message.locId,message.accountId,message.battery,message.type,[Tooler getTS]];
    BOOL result=[self.theHelper insertData:[nsql UTF8String] tableName:TABLE_MESSAGE];
    if(result)
    {
        NSLog(@"%@,addLowBattery success.",PushDataManager_Tag);
    }
    else
    {
        NSLog(@"%@,addLowBattery fail.",PushDataManager_Tag);
    }
    return result;
}
-(BOOL)addBirthday:(PMessage *)message
{
    NSLog(@"%@,addBirthday entered.",PushDataManager_Tag);
    if([self checkValid]==NO)
        return NO;
    if(message==NULL)
    {
        NSLog(@"%@,addBirthday,message is null.",PushDataManager_Tag);
        return NO;
    }
    NSString *nsql=[NSString stringWithFormat:@"insert into message(uid,special_time,msg_type,account_id,create_time) values (%@,\"%@\",%d,%@,\"%@\");",message.uid,message.specialTime,message.type,message.accountId,[Tooler getTS]];
    BOOL result=[self.theHelper insertData:[nsql UTF8String] tableName:TABLE_MESSAGE];
    if(result)
    {
        NSLog(@"%@,addBirthday success.",PushDataManager_Tag);
    }
    else
    {
        NSLog(@"%@,addBirthday fail.",PushDataManager_Tag);
    }
    return result;
    
}
-(BOOL)addFestival:(PMessage *)message
{
    NSLog(@"%@,addBirthday entered.",PushDataManager_Tag);
    if([self checkValid]==NO)
        return NO;
    if(message==NULL)
    {
        NSLog(@"%@,addBirthday,message is null.",PushDataManager_Tag);
        return NO;
    }
    NSString *nsql=[NSString stringWithFormat:@"insert into message (special_name,special_time,msg_type,account_id,create_time) values (\"%@\",\"%@\",%d,%@,\"%@\");",message.specialName,message.specialTime,message.type,message.accountId,[Tooler getTS]];
    BOOL result=[self.theHelper insertData:[nsql UTF8String] tableName:TABLE_MESSAGE];
    if(result)
    {
        NSLog(@"%@,addFestival success.",PushDataManager_Tag);
    }
    else
    {
        NSLog(@"%@,addFestival fail.",PushDataManager_Tag);
    }
    return result;
}
-(BOOL)addSimChange:(PMessage *)message
{
    NSLog(@"%@,addSimChange entered.",PushDataManager_Tag);
    if([self checkValid]==NO)
        return NO;
    if(message==NULL)
    {
        NSLog(@"%@,addSimChange,message is null.",PushDataManager_Tag);
        return NO;
    }
    NSString *nsql=[NSString stringWithFormat:@"insert into message (deviceUid,account_id,msg_type,create_time) values (%@,%@,%d,\"%@\");",message.deviceUid,message.accountId,message.type,[Tooler getTS]];
    BOOL result=[self.theHelper insertData:[nsql UTF8String] tableName:TABLE_MESSAGE];
    if(result)
    {
        NSLog(@"%@,addSimChange success.",PushDataManager_Tag);
    }
    else
    {
        NSLog(@"%@,addSimChange fail.",PushDataManager_Tag);
    }
    return result;
}
@end
